package com.kjj.user.controller;

import com.kjj.user.dto.AccountLoginDto;
import com.kjj.user.dto.SmsCodeLoginDto;
import com.kjj.user.service.ILogininfoService;
import com.kjj.user.domain.Logininfo;
import com.kjj.user.query.LogininfoQuery;
import com.kjj.basic.util.AjaxResult;
import com.kjj.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/logininfo")
public class LogininfoController {
    @Autowired
    public ILogininfoService logininfoService;

    /**
     * 保存和修改公用的
     *
     * @param logininfo 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Logininfo logininfo) {
        try {
            if (logininfo.getId() != null)
                logininfoService.update(logininfo);
            else
                logininfoService.add(logininfo);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            logininfoService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id") Long id) {
        try {
            Logininfo logininfo = logininfoService.getById(id);
            return AjaxResult.me().setData(logininfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping
    public AjaxResult list() {

        try {
            List<Logininfo> list = logininfoService.getAll();
            return AjaxResult.me().setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping
    public AjaxResult json(@RequestBody LogininfoQuery query) {
        try {
            PageList<Logininfo> pageList = logininfoService.queryPage(query);
            return AjaxResult.me().setData(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }

    /*登录接口*/
    // 1.账号登录
    @PostMapping("/account")
    public AjaxResult loginByAccount(@RequestBody AccountLoginDto dto) {
        try {
            return logininfoService.loginAccount(dto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取登录信息失败！" + e.getMessage());
        }
    }
    // 2.验证码登录
    @PostMapping("/smsCode")
    public AjaxResult loginBySmsCode(@RequestBody SmsCodeLoginDto smsCodeLoginDto) {
        try {
            return logininfoService.loginSmsCode(smsCodeLoginDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取登录信息失败！" + e.getMessage());
        }
    }
    //三方登录接口调用
    //登出
    @GetMapping("/logout/{token}")
    public AjaxResult logout(@PathVariable("token") String token) {
        try {
            return logininfoService.logout(token);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("登出失败！" + e.getMessage());
        }
    }
}
